import { observable, computed, action, runInAction, configure } from 'mobx'
import { getUserList, updateUser, addUser, deleteUser } from './UserServ.js'
import { message } from 'antd'

configure({
	enforceActions: 'observed'
})

class User {

	@observable userList = []
	@observable visible = false
	@observable status = 'add'

	@observable	username = ''
	@observable	password = ''
	@observable	isAdmin = false
	@observable _id = ''

	@action
	doGetUserList = () => {
		getUserList().then((res) => {
			runInAction(() => {
				this.userList = res.data.list
			})
		}, (err) => {
			console.log(err)
		})
	}

	@action
	doAddUser = () => {
		addUser({
			username: this.username,
			password: this.password,
			isAdmin: this.isAdmin
		}).then((res) => {
			if(res.resultCode == 0) {
				message.success('新增成功')
				this.doGetUserList()
			}
		})
	}

	@action
	doUpdateUser = (obj) => {
		updateUser(obj).then(res => {
			this.doGetUserList()
		})
	}

	@action
	doDeleteUser = (_id) => {
		deleteUser({_id}).then(res => {
			message.success('删除成功')
			this.doGetUserList()
		})
	}

}

export default User